import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken, removeToken } from './utils/auth' // get token from cookie
import { message } from 'ant-design-vue'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login','dashboard', '/auth-redirect','/register','', '/404', '/500'] // no redirect whitelist

function hasPermission(routes, path) {
  let flag = false;
  // console.log('hasp', routes)
  // debugger
  routes.map((route) => {
    if (route.path === path) {
      flag = true;
    }
  });
  return flag;
}

// router.beforeEach(async (to, from, next) => {
//   // start progress bar
//   NProgress.start()
  
//   console.log("to", to.path);
//   console.log("from", from.path);
  
//   if (to.query.kapokssotoken) {
    
//     removeToken()
//     await store
//       .dispatch('user/ssoLogin', {token:to.query.kapokssotoken})
//       .then(() => {
//         delete to.query.kapokssotoken
//         if (to.path) {
//           next({ path: to.path, query:to.query });
//         } else {
//           next({ path: "/dashboard", query:to.query });
//         }
//         NProgress.done()
//       })
//       .catch((res) => {
//         if (res.message !== '') {
//           // message.error(res.message)
//         }
//         next({ path: '/500' })
//         NProgress.done()
//       })
//   } else {
//     // determine whether the user has logged in
//     const hasToken = getToken()
//     if (hasToken) {
//       if (to.path === '/login') {
//         // if is logged in, redirect to the home page
//         next({ path: '/' })
//         NProgress.done()
//       } else {
//         // determine whether the user has obtained his permission roles through getInfo
//         const hasRoles = store.getters.roles && store.getters.roles.length > 0
//         if (hasRoles) {
//           next()
//         } else {
//           try {
//             // get user info
//             // note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
//             const res = await store.dispatch('user/getInfo')
//             const roles = res.data.data.roles
//             // generate accessible routes map based on roles
//             await store.dispatch('dict/getAllDictMap')
//             const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
//             // dynamically add accessible routes，vue-touter4取消了addRoutes方法，必须逐个添加
//             accessRoutes.map((route) => {
//               router.addRoute(route)
//             })
//             console.log(hasPermission(router.getRoutes(), to.path))
//             console.log( to.path,777777)
//             if (hasPermission(router.getRoutes(), to.path)) {
//               next({ ...to, replace: true });
//             } else {
//               next("/404");
//             }
//             // hack method to ensure that addRoutes is complete
//             // set the replace: true, so the navigation will not leave a history record
//             next({ ...to, replace: true })
//           } catch (error) {
//             // remove token and go to login page to re-login
//             await store.dispatch('user/resetToken')
//             message.error(error.message || 'Has Error')
//             next({
//               path: '/login',
//               query: {
//                 redirect: to.fullPath,
//               },
//             })
//             NProgress.done()

//           }
//         }
//       }
//     } else {
//       /* has no token*/

//       if (whiteList.indexOf(to.path) !== -1) {
//         // in the free login whitelist, go directly
//         next()
//       } else {
//         // other pages that do not have permission to access are redirected to the login page.
//         next({
//           path: '/login',
//           query: {
//             redirect: to.fullPath,
//           },
//         })
//         NProgress.done()
//       }
//     }
//   }


// })

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
  // if(loading !== undefined){
  //   loading.close();
  // }
})
